package com.yxh.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.yxh.common.annotation.Authorization;
import com.yxh.common.other.Const;
import com.yxh.exception.WordbookException;
import com.yxh.pojo.FineType;
import com.yxh.pojo.User;
import com.yxh.pojo.Wordbook;
import com.yxh.pojo.tree.BaseTreeGrid;
import com.yxh.service.UserService;
import com.yxh.service.WordbookService;
import com.yxh.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.yxh.util.APIErrorCode.INTERNAL_ERROR;
import static com.yxh.util.APIErrorCode.SUCCESS;

/**
 * Created by 32988 on 2018/6/2.
 */
@Controller
@RequestMapping("/wordbook/")
public class WordbookController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(WordbookController.class);
    private Response response;

    @Autowired
    private WordbookService wordbookService;


    /**
     * 保存
     *
     * @param wordbook
     * @return
     */
    @PostMapping("save_wordbook")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    @ResponseBody
    public Response saveWordbook(@RequestBody Wordbook wordbook) {
        wordbookService.save(wordbook);
        return Response.returnResponse(SUCCESS);
    }

    /**
     * 查询
     *
     * @param wordbook
     * @return
     */
    @GetMapping("get_information")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    @ResponseBody
    public Response getInformation(Wordbook wordbook, String page, String pageSize) {
        Integer limit;
        Integer offset;
        try {
            limit = Integer.parseInt(page);
            offset = Integer.parseInt(pageSize);
        } catch (Exception e) {
            limit = 1;
            offset = 10;
        }
        PageInfo pageInfo = wordbookService.getInformation(wordbook, limit, offset);
        response = Response.returnResponse(SUCCESS);
        response.setData(pageInfo);

        return response;
    }

    /**
     * 更新
     *
     * @param wordbook
     * @return
     */
    @RequestMapping(value = "update_wordbookInfo", method = RequestMethod.PUT)
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    @ResponseBody
    public Response updateWordbookInfo(@RequestBody Wordbook wordbook) {
        wordbookService.update(wordbook);
        response = Response.returnResponse(SUCCESS);
        return response;
    }

    /**
     * 删除
     *
     * @return
     */
    @DeleteMapping("/{id}")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    @ResponseBody
    public Response deleteWordbook(@PathVariable("id") String id) {
        wordbookService.delete(id);
        response = Response.returnResponse(SUCCESS);
        return response;
    }


    /**
     * 缺陷管理查询
     *
     * @param
     * @return
     */
    @GetMapping("getFault")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    @ResponseBody
    public Response getFault() {
        List<Wordbook> wordbooks = wordbookService.getFault();
        response = Response.returnResponse(SUCCESS);
        response.setData(wordbooks);
        return response;
    }

    @GetMapping("getWorkName")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    @ResponseBody
    public Response getWorkName(String typeCode) {
        List<Wordbook> wordbooks = new ArrayList<Wordbook>();
        List<BaseTreeGrid> wordbook = null;
        Map<String,Integer> map=new HashMap<String, Integer>();
        map.put(Const.FineTypeEnum.FXDJ.getCode(), 1);
        map.put(Const.FineTypeEnum.GCLX.getCode(), 2);
        map.put(Const.FineTypeEnum.GCXZ.getCode(), 3);
        map.put(Const.FineTypeEnum.GZPLX.getCode(), 4);
        map.put(Const.FineTypeEnum.REGION.getCode(), 5);
        map.put(Const.FineTypeEnum.HIDE.getCode(), 6);
        map.put(Const.FineTypeEnum.ELE.getCode(), 7);
        map.put(Const.FineTypeEnum.QUESTION.getCode(), 8);
        map.put(Const.FineTypeEnum.YEAR.getCode(), 9);
        response = Response.returnResponse(SUCCESS);
        if(!StringUtils.isEmpty(typeCode)){
            switch(map.get(typeCode)){
                case 1:
                    wordbooks = wordbookService.getFineType(Const.FineTypeEnum.FXDJ.getValue());
                    break;
                case 2:
                    wordbooks = wordbookService.getFineType(Const.FineTypeEnum.GCLX.getValue());
                    break;
                case 3:
                    wordbooks = wordbookService.getFineType(Const.FineTypeEnum.GCXZ.getValue());
                    break;
                case 4:
                    wordbooks = wordbookService.getFineType(Const.FineTypeEnum.GZPLX.getValue());
                    break;
                case 5:
                    wordbooks = wordbookService.getFineType(Const.FineTypeEnum.REGION.getValue());
                    break;
                case 6:
                     wordbook = wordbookService.getFineTypeTree(Const.FineTypeEnum.HIDE.getValue());
                    response.setData(wordbook);

                    break;
                case 9:
                    wordbooks = wordbookService.getFineType(Const.FineTypeEnum.YEAR.getValue());
                    break;
                case 7:
                    wordbook = wordbookService.getFineTypeTree(Const.FineTypeEnum.ELE.getValue());
                    response.setData(wordbook);

                    break;
                case 8:
                    wordbook = wordbookService.getFineTypeTree(Const.FineTypeEnum.QUESTION.getValue());
                    response.setData(wordbook);

                    break;
                default:
                    wordbooks = wordbookService.getFineType(Const.FineTypeEnum.FAULT.getValue());
                    break;
            }
        }

        response.setData(wordbooks);
        if(wordbook!=null){
            response.setData(wordbook);
        }

        return response;
    }
}
